﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using LimitOrderBookSim.Model.Markets;
using LimitOrderBookSim.Assets;

namespace LimitOrderBookSim.Commons
{
    public static class Builder
    {

        static List<Market> _markets = null;

        public static List<Market> Markets
        {
            get
            {
                if (null == _markets)
                {
                    Build();
                }

                return _markets;
            }

        }


        private static void Build()
        {
            _markets =
                   (from r in XElement.Load("./Datas/Markets.xml").Descendants("MARKET")
                    select new Market()
                    {
                        Name = r.Attribute("name").Value,
                        Events = (from ev in r.Descendants("EVENT")
                                    select new Event()
                                    {
                                        Expiration = DateTime.Parse(ev.Attribute("expiration").Value),
                                        Name = ev.Attribute("name").Value,
                                        Contract = (from contract in ev.Descendants("CONTRACT")
                                                      select new Contract()
                                                        {
                                                            Name = contract.Attribute("name").Value,
                                                            Pool = new OrderPool()
                                                        }).ToList()
                                    }).ToList()
                    }).ToList();
        }
    }
}
